package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "站内信接口")
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IUserService userService ;
    @Resource
    private IMessageService messageService;

    /**
     * 发送站内信
     *
     * @param receiveUserId 接收⽤⼾Id
     * @param content 内容
     * @return AppResult
     */
    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send (HttpServletRequest request
            , @ApiParam(value = "接收⽤⼾Id") @RequestParam(value = "receiveUserId") @NonNull Long receiveUserId
            , @ApiParam(value = "站内信内容") @RequestParam(value = "content") @NonNull String content) {
        HttpSession session = request.getSession(false);
        User postUser = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (postUser.getState() == 1) {
            // 当前用户被禁言
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        if (postUser.getId() == receiveUserId) {
            log.warn("无法给自己发送信息");
            return AppResult.failed("无法给自己发送信息");
        }
        User receivedUser = userService.selectById(receiveUserId);
        if (receivedUser == null || receivedUser.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED);
        }
        Message message = new Message();
        message.setPostUserId(postUser.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success(ResultCode.SUCCESS);
    }
    /**
     * 获取未读消息个数
     *
     * @return AppResult<Integer>
    1
    2
    3
    4
    比特就业课
     */
    @ApiOperation("获取未读消息个数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount (HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        // 用户不存在
        if (user == null || user.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED);
        }
        if (user.getId() == null || user.getId() <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }

    @ApiOperation("查询⽤⼾的所有站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        // 用户不存在
        if (user == null || user.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED);
        }
        if (user.getId() == null || user.getId() <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }
    @ApiOperation("将站内信标为已读")
    @PostMapping("/markRead")
    public AppResult markRead (HttpServletRequest request ,@ApiParam("站内信id")@RequestParam("id")@NonNull Long id) {
        if (id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED);
        }
        //站内信是否存在,站内信的拥有者是否是当前登录用户
        Message message = messageService.selectById(id);
        if (message == null || message.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId() != message.getReceiveUserId()) {
            // 当前登录用户和短信的接收者不一致
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        messageService.updateStateById(id,(byte)1);
        return AppResult.success();
    }



}
